package com.hunttown.mes.manage.controller._basic;

import com.hunttown.mes.common.utils.DateConvertUtils;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.manage.controller.common.ModelService;
import com.hunttown.mes.manage.service.AnalysisManageManageService;
import com.hunttown.mes.rpc.domain.AnalysisManageDTO;
import com.hunttown.mes.rpc.domain.enums.api.ClientTypeEnum;
import com.hunttown.common.domain.Page;
import com.hunttown.mes.manage.service.AnalysisManageLoginLogManageService;
import com.hunttown.mes.rpc.domain.AnalysisManageLoginLogDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisManageLoginLogDTOQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import static com.hunttown.mes.common.utils.PageUtils.PagePackage;

/**
 * @title: 管理员登录日志
 * @author: wangjunfu
 * @date: 2019-08-12 09:57:54
 * @description:
 */
@Controller
@RequestMapping(value = "/basic/logadmin")
public class ManageLoginLogController {

    private final static Logger logger = LoggerFactory.getLogger(ManageLoginLogController.class);

    private final AnalysisManageLoginLogManageService loginLogService;
    private final ModelService modelService;
    private final AnalysisManageManageService manageService;

    @Autowired
    public ManageLoginLogController(AnalysisManageLoginLogManageService loginLogService, ModelService modelService, AnalysisManageManageService manageService) {
        this.loginLogService = loginLogService;
        this.modelService = modelService;
        this.manageService = manageService;
    }

    // 首页
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model, AnalysisManageLoginLogDTOQuery dtoQuery) {

        //通过用户姓名查询
        if (StringUtils.isNotBlank(dtoQuery.getAdminName())) {
            AnalysisManageDTO admin_dto = manageService.getByNickName(dtoQuery.getAdminName().trim());
            if (admin_dto != null) {
                dtoQuery.setAdminId(admin_dto.getId());
            }
        }

        //日期（初始化 2天）
        if (StringUtils.isBlank(dtoQuery.getDaterange())) {
            dtoQuery.setDaterange(String.format("%s ~ %s", DateConvertUtils.getPastDate(1), DateConvertUtils.getPastDate(0)));
        }

        dtoQuery.setOrderBy("order by id desc");
        Page<AnalysisManageLoginLogDTO> dataPage = loginLogService.getForPage(dtoQuery);

        //实时更新列表信息
        if (dataPage != null && dataPage.getItems().size() > 0) {
            for (AnalysisManageLoginLogDTO item : dataPage.getItems()) {
                item.setAdminName(modelService.getManageName(item.getAdminId()));

                //客户端类型
                item.setClientTypeName(ClientTypeEnum.getEnumByKey(item.getClientType()));
            }
        }

        model.addAttribute("page", dataPage);
        model.addAttribute("PagePackage", PagePackage(dataPage.getPn(), dataPage.getStart(), dataPage.getPageSize(), dataPage.getTotalPages(), dataPage.getTotalItems()));
        model.addAttribute("dtoQuery", dtoQuery);

        //日期
        if (StringUtils.isNotBlank(dtoQuery.getDaterange())) {
            String[] date = dtoQuery.getDaterange().split("~");
            if (date.length == 2) {
                model.addAttribute("startDate", date[0].trim());
                model.addAttribute("endDate", date[1].trim());
            }
        }

        //客户端类型
        model.addAttribute("clientTypeList", ClientTypeEnum.getEnumList());

        return "basic/logadmin/index";
    }

    //region 数据查看

    /**
     * 数据查看
     *
     * @param model     model模型
     * @param id        主键id
     * @param tableName 数据库英文表名
     * @return
     */
    @RequestMapping(value = "/show", method = RequestMethod.POST)
    public String show(Model model, Integer id, String tableName) {
        id = (id == null ? 0 : id);

        AnalysisManageLoginLogDTO obj = new AnalysisManageLoginLogDTO();
        obj.setId(id);

        if (id > 0) {
            obj = loginLogService.getById(id);
        }
        loginLogService.assembleObjInfo(model, tableName, obj);

        return "show/show-template";
    }

    //endregion
}